Repository navigation
gh-161962: Fix LazyLoader deadlock on circular imports between threads - #161990
Open
tanveer-arch wants to merge 2 commits into
Open
tanveer-arch wants to merge 2 commits into
tanveer-arch wants to merge 2 commits into
Conversation
tanveer-arch
requested review from
FFY00,
brettcannon,
ericsnowcurrently,
ncoghlan and
warsaw
as code owners
November 14, 2026 06:59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #161962.
LazyLoaderserialized the lazy load with a plain per-modulethreading.RLock, which has no deadlock detection. If two lazily loaded modules import each other and two threads first-touch a different one, each thread takes its own module's lock and then blocks on the other's lock forever. The same modules imported eagerly do not deadlock, because the import system's module lock detects the cycle and raises_DeadlockError, and the partially initialized module is used instead.This makes the lazy load use the import system's module lock (
_bootstrap._ModuleLock) too. On a_DeadlockErrorit proceeds without the lock, exactly as_lock_unlock_module()does for eager imports, so the thread already loading the module returns the partially initialized module through the existingis_loadingbranch. The per-moduleRLockand the deferredthreadingimport inexec_module()are removed.Adds a regression test (
test_lazy_circular_import_no_deadlock) that loads two lazily loaded modules in a cycle from two threads, with a barrier for deterministic timing, and asserts both complete with the expected values. The test deadlocks without the fix.The fix follows the approach suggested by @djrobstep in the issue. AI assistance disclosure: I used an AI tool to help investigate and build this fix; I reviewed the change and the test in detail and verified them by building CPython from main and running the importlib test suite.
importlib.util.LazyLoaderdeadlocks when two threads first-touch modules in an import cycle #161962